import { wxRequest } from '../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
Page({
  data: {
    imgUrl: app.globalData.imgUrl,
    uploadUrl: wx.getStorageSync('uploadUrl'),
    id: null,
    showModal: false,
    score:5,
    contentVideo:'',
    itemid:''
  },


  onLoad: function (options) {
    console.log(options, 'options')
    if (options.id) {
      this.setData({
        id: options.id,
        itemid: options.itemid,
        assesstype: options.assesstype
      })
    }
    setTimeout(()=>{
      this.contentImgs = this.selectComponent("#contentImgs");
      this.contentVideo = this.selectComponent("#contentVideo") 
    },500)
    
  },
  onShow: function () {
    this.getdetail()
  },
  onChange:function(e){ 
    this.setData({
      score:e.detail
    })
  },

  //选择介绍图片
  choosecontentImgs: function (e) {
    this.setData({
      imgs: this.contentImgs.data.imgs
    })
  },

  deletecontentImgs: function (e) {
    this.setData({
      imgs: this.contentImgs.data.imgs
    })
  },

  delorigincontentImgs: function () {
    this.setData({
      originImgs: this.contentImgs.data.originImgs
    })
  },


  //介绍视频回调
  contentVideoCallback: function () {
    this.setData({
      contentVideo: this.contentVideo.data.video
    })
  },



  //获取详情
  getdetail: function () {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/order/detail?id=' + this.data.id,
      method: 'POST',
    }, (res) => {
      var resVo = res.vo
      if (typeof resVo.detailJson == 'string') {
        resVo.detailJson = JSON.parse(resVo.detailJson)
      }
      this.setData({
        detail: resVo
      })
    })
  },


  // 顾客评价
  formSubmit:function(e){
    var formdata = e.detail.value;
    formdata.score = this.data.score
    formdata.video = this.data.contentVideo
    formdata.assessImgList = this.contentImgs.data.datalist
    // formdata.pid = this.data.id
    formdata.itemId = this.data.itemid

    console.log(formdata, 'formdata')

    if (formdata.score<1){
      wx.showToast({
        icon:'none',
        title: '星级不能为空',
      })
      return
    }
 
    if (!formdata.content) {
      wx.showToast({
        icon: 'none',
        title: '请填写评语',
      })
      return
    }
 
    this.assessRequset(formdata)
  },


  assessRequset: function (params){
    var url = '/odr/protected/assess/create'
    if (this.data.assesstype==1){
      url = '/odr/protected/assess/append'
    }

    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl +url,
      method: 'POST',
      data:params
    }, (res) => {
      wx.showToast({
        title: '评论成功',
      })

      setTimeout(()=>{
        wx.redirectTo({
          url: '/pages/shop_order/index',
        })
      },1000)
    })
  }


})